﻿
//表单附件选择插件
(function ($, ibpm) {
    "use strict";
    $.BPM_Pic = {
        init: function ($self) {
            var dfop = $self[0]._MPic.dfop;
            $.BPM_Pic.initRender($self, dfop);
        },
        initRender: function ($self, dfop) {
            $self.attr('type', 'pic').addClass('M-Pic-wrap');
            var $wrap = $('<div class="M-Uploader-input" ></div>');

            var $btnGroup = $('<div class="M-Uploader-btn-group"></div>');
            var $openBtn = $('<a id="M-Uploader_uploadBtn_' + dfop.id + '" class="btn btn-success M-Uploader-input-btn">图片管理</a>');

            if (dfop.isOnlyRead)
                $wrap.text("只读");
            else
                $wrap.text("编辑");

            $self.append($wrap);
            $btnGroup.append($openBtn);
            $self.append($btnGroup);

            if (dfop.isOnlyRead)
                $openBtn.on('click', $.BPM_Pic.openShowOnlyPic);
            else
                $openBtn.on('click', $.BPM_Pic.openShowPic);
        },
        openShowPic: function () {
            var $btn = $(this);
            var $self = $btn.parents('.M-Pic-wrap');
            var dfop = $self[0]._MPic.dfop;
            ibpm.layerForm({
                id: 'layer_' + dfop.id,  //id不能重复
                title: dfop.placeholder,
                url: top.$.rootUrl + '/Annexes/ShowPic?id=' + dfop.value,
                width: 900,
                height: 700,
                btn: null,
                maxmin: true
            });
        },
        openShowOnlyPic: function () {
            var $btn = $(this);
            var $self = $btn.parents('.M-Pic-wrap');
            var dfop = $self[0]._MPic.dfop;
            ibpm.layerForm({
                id: 'layer_' + dfop.id,  //id不能重复
                title: dfop.placeholder,
                url: top.$.rootUrl + '/Annexes/ShowOnlyPic?id=' + dfop.value,
                width: 860,
                height: 620,
                btn: null,
                maxmin: true
            });
        }
    };

    $.fn.ibpm_pic = function (op) {
        var $this = $(this);
        if (!!$this[0]._MPic) {
            return $this;
        }
        var dfop = {
            placeholder: '图片管理',
            isOnlyRead: false,
            value: false,
            extensions: ''
        }

        $.extend(dfop, op || {});
        dfop.id = $this.attr('id');
        if (!dfop.value) {
            dfop.value = ibpm.newGuid();
        }

        $this[0]._MPic = { dfop: dfop };
        $.BPM_Pic.init($this);
    };

    $.fn.ibpm_picSet = function (value) {
        var $self = $(this);
        var dfop = $self[0]._MPic.dfop;
        dfop.value = value;
    }

    $.fn.ibpm_picGet = function () {
        var $this = $(this);
        var dfop = $this[0]._MPic.dfop;
        return dfop.value;
    }

})(jQuery, top.ibpm);